package com.ryder.book_cycle.controller;

import com.ryder.book_cycle.common.response.Result;
import com.ryder.book_cycle.entity.BookOrder;
import com.ryder.book_cycle.entity.BookOrderDTO;
import com.ryder.book_cycle.service.BookOrderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Objects;
import java.util.Map;

/**
 * 描述: 订单表(BookOrder)表控制层
 * 
 * @author Ryder
 * @version 1.0
 * @since 1.0
 * 更新日期: 2025-07-13 00:05:18
 */
@RestController
@RequestMapping("/bookOrder")
public class BookOrderController {

    private static final Logger log = LoggerFactory.getLogger(BookOrderController.class);

    @Autowired
    private BookOrderService bookOrderService;

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result<Page<BookOrderDTO>> pageList(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam Long merchantId,
            @RequestParam(required = false) String bookName,
            @RequestParam(required = false) String buyerName) {

        try {
            Page<BookOrder> page = new Page<>(pageNum, pageSize);
            Page<BookOrderDTO> result = bookOrderService.selectOrderPage(page, bookName, buyerName, merchantId);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("分页查询失败：{}", e.getMessage(), e);
            return Result.fail("分页异常");
        }
    }

    @GetMapping("/userPage")
    public Result<Page<BookOrderDTO>> userPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "20") int pageSize,
                @RequestParam Long buyerId,
            @RequestParam(required = false) String bookName) {

        try {
            Page<BookOrder> page = new Page<>(pageNum, pageSize);
            Page<BookOrderDTO> result = bookOrderService.selectUserOrderPage(page, bookName, buyerId);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("分页查询失败：{}", e.getMessage(), e);
            return Result.fail("分页异常");
        }
    }

    /**
     * 根据主键查询
     */
    @GetMapping("/{id}")
    public Result<BookOrder> queryById(@PathVariable Long id) {
        try {
            BookOrder entity = bookOrderService.selectOrderDetailById(id);
            return entity != null ? Result.ok(entity) : Result.fail("未找到数据");
        } catch (Exception e) {
            log.error("查询失败：{}", e.getMessage(), e);
            return Result.fail("查询异常");
        }
    }

    /**
     * 新增
     */
    @PostMapping
    public Result<Boolean> add(@RequestBody BookOrder entity) {
        try {
            boolean saved = bookOrderService.add(entity);
            return saved ? Result.ok(true) : Result.fail("新增失败");
        } catch (Exception e) {
            log.error("新增失败：{}", e.getMessage(), e);
            return Result.fail("新增异常");
        }
    }

    /**
     * 修改
     */
    @PutMapping
    public Result<Boolean> update(@RequestBody BookOrder entity) {
        try {
            if (entity.getStatus() == 2) {
                return bookOrderService.updateBookOrder(entity, 1L) ? Result.ok(true) : Result.fail("更新失败");
            }
            boolean updated = bookOrderService.updateBookOrder(entity, 0L);
            return updated ? Result.ok(true) : Result.fail("更新失败");
        } catch (Exception e) {
            log.error("更新失败：{}", e.getMessage(), e);
            return Result.fail("更新异常");
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        try {
            boolean removed = bookOrderService.removeById(id);
            return removed ? Result.ok(true) : Result.fail("删除失败");
        } catch (Exception e) {
            log.error("删除失败：{}", e.getMessage(), e);
            return Result.fail("删除异常");
        }
    }

}
